package com.ruoyi.web.controller.course;

import com.ruoyi.chapter.domain.CourseChapter;
import com.ruoyi.chapter.service.ICourseChapterService;
import com.ruoyi.chapter.service.IUserChapterService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.enums.BusinessType;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.course.domain.Course;
import com.ruoyi.course.service.ICourseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 课程管理Controller
 *
 * @author ruoyi
 * @date 2025-11-04
 */
@RestController
@RequestMapping("/course/course")
public class CourseController extends BaseController
{
    @Autowired
    private ICourseService courseService;
    @Autowired
    private ICourseChapterService iCourseChapterService;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private IUserChapterService userChapterService;

    /**
     * 查询课程管理列表
     */
//    @PreAuthorize("@ss.hasPermi('course:course:list')")
    @GetMapping("/list")
    public TableDataInfo list(Course course)
    {
        startPage();
        List<Course> list = courseService.selectCourseList(course);
        return getDataTable(list);
    }

    /**
     * 导出课程管理列表
     */
//    @PreAuthorize("@ss.hasPermi('course:course:export')")
    @Log(title = "课程管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Course course)
    {
        List<Course> list = courseService.selectCourseList(course);
        ExcelUtil<Course> util = new ExcelUtil<Course>(Course.class);
        util.exportExcel(response, list, "课程管理数据");
    }

    /**
     * 获取课程管理详细信息
     */
//    @PreAuthorize("@ss.hasPermi('course:course:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(courseService.selectCourseById(id));
    }

    /**
     * 新增课程管理
     */
//    @PreAuthorize("@ss.hasPermi('course:course:add')")
    @Log(title = "课程管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Course course)
    {
        return toAjax(courseService.insertCourse(course));
    }

    /**
     * 修改课程管理
     */
//    @PreAuthorize("@ss.hasPermi('course:course:edit')")
    @Log(title = "课程管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Course course)
    {
        return toAjax(courseService.updateCourse(course));
    }

    /**
     * 删除课程管理
     */
//    @PreAuthorize("@ss.hasPermi('course:course:remove')")
    @Log(title = "课程管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(courseService.deleteCourseByIds(ids));
    }

    /**
     * 通过年级的id获得课程列表，用在推荐课程的页面
     */
    @GetMapping("/getByGradeId/{id}")
    public AjaxResult getByGradeId(@PathVariable Long id){
//        System.err.println(id);
        if (id==0){
            List<Course> list = courseService.getList();
            return success(list);
        }else {
            List<Course> courseList = courseService.getCourseByGradeId(id);
//        System.err.println(courseList);
            return success(courseList);
        }
    }

    /**
     * 通过年级的id和userId获得课程列表，用在用户首页
     */
    @GetMapping("/getByGradeIdAndUserId/{id}")
    public AjaxResult getByGradeIdAndUserId(@PathVariable Long id){
        Long userId = redisCache.getCacheObject("userId:");
        if (id==0){
            List<Long> cids = userChapterService.getCidsByUid(userId);
            ArrayList<Course> list = new ArrayList<>();
            for (Long cid : cids) {
                Course courseDesById = courseService.getCourseDesById(cid);
                list.add(courseDesById);
            }
//        System.err.println(list);
        return success(list);
        }else {
            List<Course> courseList = courseService.getCourseByUserIdAndGradeId(userId, id);
            System.err.println(courseList);
//            return null;
            return success(courseList);
        }
    }

    @GetMapping("/getByCateGoryId/{id}")
    public AjaxResult getByCategoryById(@PathVariable Long id){
//        System.err.println(id);
        List<Course> courseList = courseService.getCourseByCategoryId(id);
//        System.err.println(courseList);
        return success(courseList);
    }

    @GetMapping("/getCourseDesById/{id}")
    public  AjaxResult getCourseDesById(@PathVariable Long id){

//        System.err.println("----------"+id);
        Course course = courseService.getCourseDesById(id);
//        System.err.println(courseList);
        return success(course);
    }


    @GetMapping("/getCourseAndCategoryById/{id}")
    public AjaxResult getCourseAndCategoryById(@PathVariable Long id){
//        System.err.println(id);
        List<CourseChapter> courseChapterDTOList = iCourseChapterService.getCourseAndCategoryById(id);
//        System.err.println(courseChapterDTOList);
        return success(courseChapterDTOList);
    }
    /**
     * 通过name搜索课程 推荐页面 不涉及报名课程
     * */
    @GetMapping("/handleSearchCourseByName")
    public  AjaxResult handleSearchCourseByName(String name){
//        System.err.println(name);
        List<Course> courseList = courseService.handleSearchCourseByName(name);
//        System.err.println(courseList);
        return success(courseList);
    }
    /**
     * 通过userID获得课程全查询
     * */
    @GetMapping("/listByUserId")
    public AjaxResult getListByUserId(){
//        获取UserId
        Long userId = redisCache.getCacheObject("userId:");
//        System.err.println(userId);
        List<Long> cids = userChapterService.getCidsByUid(userId);
        ArrayList<Course> list = new ArrayList<>();
        for (Long cid : cids) {
            Course courseDesById = courseService.getCourseDesById(cid);
            list.add(courseDesById);
        }
//        System.err.println(list);
        return success(list);
    }
}
